package org.jcc.core.cs.abstraction;

import java.util.Random;

public class SortDemoV3 {
	public static void main(String[] args) {
		// 生成随机数组
		int[] arr = makeRandomArray(10, 100);
		// 排序
		sort(arr);
	}

	private static int[] makeRandomArray(int size, int bound) {
		int[] arr = new int[size]; Random r = new Random();
		for (int i = 0; i < arr.length; i++)
			arr[i] = r.nextInt(bound);
		printArray(arr);
		return arr;
	}

	private static void sort(int[] arr) {
		int temp = 0, length = arr.length;
		for (int i = 0; i < length; length--)
			for (int j = i + 1; j < length; j++)
				if (arr[j - 1] > arr[j]) {
					temp = arr[j - 1]; arr[j - 1] = arr[j];	arr[j] = temp;
				}
		printArray(arr);
	}

	private static void printArray(int[] arr) {
		for (int i = 0; i < arr.length; i++)
			if (i == 0) System.out.print("[" + arr[i] + ", ");
			else if (i < arr.length - 1) System.out.print(arr[i] + ", ");
			else System.out.println(arr[i] + "]");
	}
}
